package CreationTools;

import java.awt.Point;
import java.awt.event.MouseEvent;

import org.jhotdraw.framework.DrawingEditor;
import org.jhotdraw.framework.Figure;
import org.jhotdraw.standard.CreationTool;

import FloorComponents.Grid;
import FloorComponents.Proxy;
import FloorComponents.Window;

public class WindowTool extends CreationTool {

	private Grid _grid;
	private Proxy _proxy;
	
	public WindowTool(DrawingEditor newDrawingEditor, Figure prototype, Proxy grid) {
		super(newDrawingEditor, prototype);
		_proxy = grid;
		_grid = _proxy.get();
	}

	protected WindowTool(DrawingEditor newDrawingEditor) {
		super(newDrawingEditor);
	}
	
	// look into grid to see if it works right
	public void mouseDrag(MouseEvent e, int x, int y){
		if (getAddedFigure() != null /*&& _grid.check(getAnchorX(),getAnchorY())*/) {
			
			System.out.print("enters conditional");
			
			boolean isHor = false;
			
			if (_grid.check(getAnchorX()-5,getAnchorY()) && _grid.check(getAnchorX()+5, getAnchorY()) && (!_grid.check(getAnchorX(), getAnchorY()-5) || !_grid.check(getAnchorX(), getAnchorY()+5)))
				isHor = true;
			
			
			if (isHor){
				int top = getAnchorY();
				while (_grid.check(getAnchorX(), top))
					top--;
				getAddedFigure().displayBox(new Point(getAnchorX()-10,top), new Point(getAnchorX()+10, top+5));
				getAddedFigure().displayBox(new Point(getAnchorX()-10, top+5), new Point(getAnchorX()+10, top+10));
			}
			else {
				int left = getAnchorX();
				while(_grid.check(left, getAnchorY()))
					left--;
				getAddedFigure().displayBox(new Point(left, getAnchorY()-10), new Point(left+5, getAnchorY()+10));
				getAddedFigure().displayBox(new Point(left+5, getAnchorY()-10), new Point(left+10, getAnchorY()+10));
			}
		}
		_proxy.update(_grid);
	}
}